from django.shortcuts import render

from rest_framework.viewsets import ViewSet
from rest_framework.response import Response
from django.contrib.auth.hashers import make_password

from .models import SendModel
from users.models import UserModel
from shops.models import ShopsModel

class SendView(ViewSet):

    def register_send(self,request):

        name = request.data.get('name')
        mobile = request.data.get('mobile')
        password = request.data.get('password')
        code = request.data.get('code')

        try:
            if not [name,mobile,password,code]:
                return Response({'errcode':2,'msg':'用户信息不完整'})

            user = UserModel.objects.filter(mobile=mobile,is_delete=False).first()
            if user:
                return Response({'errcode':1,'msg':'用户已注册'})

            user = ShopsModel.objects.filter(mobile=mobile, is_delete=False).first()
            if user:
                return Response({'errcode': 1, 'msg': '用户已注册'})

            pwd = make_password(password,'pbkdf2_sha256')
            SendModel.objects.create(name=name, password=pwd, mobile=mobile)  # 默认普通用户
            return Response({'errcode':0,'msg':'注册成功'})
        except Exception as e:
            print(str(e))
            return Response({'errcode':3,'msg':'服务器错误'})

